<?php
/**
 * 用户控制器
 */
namespace Home\Controller;

class UserController extends BaseController
{

    public function index()
    {
        $this->display();
    }
    public function getUserListByPage()
    {
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $pageIndex = I('post.page');
        $pageSize  = I('post.rows');
        $username  = I('post.username');
        $where['level']  = array('EQ', 0);
        if ($username != '') {
            $where['username'] = array('like', '%' . $username . '%');
        }
        $count    = M('user')->where($where)->count();
        $startno  = ($pageIndex - 1) * $pageSize;
        $limit    = $startno . ',' . $pageSize;
        $userList = M('user')->where($where)->order('id desc')->limit($limit)->select();
        //必须将记录总数和记录分别返回
        $result = array(
            "total" => $count, //记录总数
            "rows"  => $userList, //记录
        );
        $this->ajaxReturn($result, 'JSON');
    }
    public function resetPwd()
    {
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db   = M('user');
        $data = array(
            'password' => md5('123456'),
        );
        $where['id'] = array('in', I('post.ids', 'intdiv'));
        if ($db->where($where)->save($data)) {
            $result = array("msg" => "操作成功");
        } else {
            $result = array("msg" => "已重置成功，请不要多次重复操作");
        }
        $this->ajaxReturn($result, 'JSON');
    }
    public function editUserStatus()
    {
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db   = M('user');
        $lock = I('post.lock', 'intdiv');
        if ($lock == 1) {
            $lock = 0;
        } else {
            $lock = 1;
        }
        $data = array(
            'lock' => $lock,
        );
        $where['id'] = array('in', I('post.ids', 'intdiv'));
        if ($db->where($where)->save($data)) {
            $result = array("msg" => "操作成功", "errorCode" => 0);
        } else {
            $result = array("msg" => "已操作成功，请不要重复多次操作", "errorCode" => 1);
        }
        $this->ajaxReturn($result, 'JSON');
    }
    public function SaveUser()
    {
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        if (I('post.password') != I('post.confirmpwd')) {
            $result = array("msg" => "密码与确认密码不一致", "errorCode" => 2);
            $this->ajaxReturn($result, 'JSON');
        }
        $db   = M('user');
        $data = array(
            "username" => I('post.add_username'),
            "password" => md5(I('post.password')),
        );
        $id = $db->add($data);
        if ($id) {
            $result = array("msg" => "操作成功", "errorCode" => 0);
        } else {
            $result = array("msg" => "操作失败", "errorCode" => 1);
        }
        $this->ajaxReturn($result, 'JSON');
    }
    public function role(){
        $this->display();
    }
    public function doSaveRole(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $id = I('post.id');
        $rolename = I('post.rolename');
        $roledesc = I('post.roledesc');
        $data = array(
            'role_name'=>$rolename,
            'role_desc'=>$roledesc
        );
        if($id == -1){
            $ret = M('role')->add($data);
        }else{
            $where['id']=$id;
            $ret = M('role')->where($where)->save($data);
        }
        if($ret){
            $result = array("msg" => "操作成功", "errorCode" => 0);
        }else{
            $result = array("msg" => "操作失败", "errorCode" => 1);
        }
        $this->ajaxReturn($result, 'JSON');
    }
    public function getRoleListByPage(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $rolename  = I('post.rolename');
        $pageIndex = I('post.page');
        $pageSize  = I('post.rows');
        $where['role_flag'] = 0;
        if ($rolename != '') {
            $where['role_name'] = array('like', '%' . $rolename . '%');
        }
        $count    = M('role')->where($where)->count();
        $startno  = ($pageIndex - 1) * $pageSize;
        $limit    = $startno . ',' . $pageSize;
        $roleList = M('role')->where($where)->limit($limit)->select();
        //必须将记录总数和记录分别返回
        $result = array(
            "total" => $count, //记录总数
            "rows"  => $roleList, //记录
        );
        $this->ajaxReturn($result, 'JSON');
    }
    public function getRoleList(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $where['role_flag'] = 0;
        $roleList = M('role')->field('id,role_name')->where($where)->select();
        $this->ajaxReturn($roleList, 'JSON');
    }
    public function getAllMenu(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $item = [];
        $authMenu = M('auth')->field('id,auth_name,iconCls,state,pid,module,controller,method')->select();
        if($authMenu){
            foreach ($authMenu as $key => $value) {
                $item['id'] = $value['id'];
                $item['text'] = $value['auth_name'];
                $item['state'] = $value['state'];
                $item['pid'] = $value['pid'];
                $item['iconCls'] = $value['iconCls'];
                $item['attributes'] = array('module' => $value['module'],'controller'=>$value['controller'],'method'=>$value['method']);
                $data[] = $item;
            }
            $this->ajaxReturn($data, 'JSON');
        }else{
            $this->ajaxReturn(array(), 'JSON');
        }
    }
    public function doDisAuth(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $id = I('post.id');
        $auth_ids = I('post.auth_ids');
        $auth_desc = I('post.auth_desc');
        $data = array(
            'auth_ids' => $auth_ids,
            'auth_desc' => $auth_desc
        );
        $where = array('id' => $id);
        if (M('role')->where($where)->save($data)) {
            $this->ajaxReturn(array("errorCode"=>0,"msg"=>"操作成功"),"JSON");
        }else{
            $this->ajaxReturn(array("errorCode"=>1,"msg"=>"操作失败"),"JSON");
        }
    }
    public function doSaveUserRole(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $rolelist = I('post.rolelist');
        $userid = I('post.userid');
        //先删除以前的角色
        M('user_role')->where(array('user_id'=>$userid))->delete();
        //然后重新添加
        if($rolelist != ''){
            $role_id = explode(',',$rolelist);
            $role = M('user_role');
            for($i=0;$i<count($role_id);$i++){
                $role -> add(array('user_id'=>$userid,'role_id'=>$role_id[$i]));
            }
        }
        $this->ajaxReturn(array("errorCode"=>0,"msg"=>"操作成功"),"JSON");
    }
    public function getUserRole(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $userid = I('post.userid');
        $result = M('user_role')->field('role_id')->where(array('user_id'=>$userid))->select();
        $this->ajaxReturn($result, 'JSON');
    }
}
